<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     *
     * @throws \Exception
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Exception
     */
    public function render($request, Exception $exception)
    {

        if ($exception instanceof ValidationException){
            //request类验证异常时返回具体信息，而不是The given data was invalid 500异常
            //这类异常信息用getMessage()获取不到
            //dd($e->validator->errors()->first());

            return response()->json([
                'msg' => $exception->validator->errors()->first(),
                'errors' => $exception->validator->getMessageBag(),
                'code' => $exception->getCode(),
                'success' => false,
                'exception' => get_class($exception),
                'file' => $exception->getFile(),
                'line' => $exception->getLine()
            ], 422);
        }



        if ($exception instanceof ApiException){
            $result = [
                "msg" => $exception->getMessage(),
                "data"=>$exception->getMessage(),
                "status"=>0,
                'code'=>$exception->getCode()
            ];
            return response()->json($result);
        }




        if($exception->getMessage()=="")
        $result = [
            "msg" => "Route Not Find",
            "data"=>$exception->getMessage(),
            "status"=>0,
            'code'=>$exception->getCode()
        ];
        else
            $result = [
                "msg" => $exception->getMessage(),
                "data"=>null,
                "status"=>0,
                'code'=>$exception->getCode()
            ];
        return response()->json($result);
       // return parent::render($request, $exception);
    }

}
